import {
  Radio,
  Select,
  DatePicker,
  Button,
  Breadcrumb,
  Form,
  Card,
  Table,
  Tag,
  Space,
  Popconfirm,
} from "antd";
import { EditOutlined, DeleteOutlined } from "@ant-design/icons";
import img404 from "../../assets/logo192.png";
import { Link, useNavigate } from "react-router-dom";
import locale from "antd/es/date-picker/locale/zh_CN";
import "moment/locale/zh-cn";
import { useEffect, useState } from "react";
import "./index.scss";
import { http } from "../../utils";
import { useStore } from "../../store";
import { observer } from "mobx-react-lite";

const { Option } = Select;
function Manage() {
  const { ChannelStore } = useStore();

  const { RangePicker } = DatePicker;
  const [value, setValue] = useState(-1);
  const onChange = (e) => {
    setValue(e.target.value);
  };
  // 文章列表管理
  const [articleData, setArticleData] = useState({
    list: [],
    count: 0,
  });
  // 文章参数管理
  const [params, setParams] = useState({
    page: 1,
    per_page: 10,
  });
  const pageChange = (page) => {
    setParams({
      ...params,
      page,
    });
  };
  useEffect(() => {
    const lodaList = () => {
      http
        .get("/mp/articles", {
          params,
        })
        .then((res) => {
          console.log(res);
          setArticleData({
            list: res.data.data.results,
            count: res.data.data.total_count,
          });
        });
    };
    lodaList();
  }, [params]);

  // const [channeList, setChanneList] = useState([]);
  // useEffect(() => {
  //   const fetchChannels = async () => {
  //     const res = await http.get("/channels");
  //     // console.log(res.data.data.channels);
  //     setChanneList(res.data.data.channels);
  //   };
  //   fetchChannels();
  // }, []);

  // 删除功能
  const confirm = (data) => {
    console.log(data);
    http.delete(`/mp/articles/${data.id}`);
    // 刷新列表
    setParams({
      ...params,
      page: 1,
    });
  };
  // 编辑
  const navigate = useNavigate();
  const goSend = (data) => {
    console.log(data);
    navigate(`/send?id=${data.id}`);
  };
  const onFinish = (values) => {
    console.log(values);
    const { chinel_id, time, state } = values;
    // 处理数据
    const _params = {};
    if (state !== -1) {
      _params.state = state;
    }
    if (chinel_id) {
      _params.chinel_id = chinel_id;
    }
    if (time) {
      _params.begin_pubdate = time[0].format("YYYY-MM-DD");
      _params.end_pubdate = time[1].format("YYYY-MM-DD");
    }
    console.log(_params);
    setParams({ ...params, ..._params });
  };

  const columns = [
    {
      title: "封面",
      dataIndex: "cover",
      // width: 120,
      render: (cover) => {
        return (
          <img src={cover.images[0] || img404} width={80} height={60} alt="" />
        );
      },
    },
    {
      title: "标题",
      dataIndex: "title",
      width: 220,
    },
    {
      title: "状态",
      dataIndex: "status",
      render: (data) => <Tag color="green">审核通过</Tag>,
    },
    {
      title: "发布时间",
      dataIndex: "pubdate",
    },
    {
      title: "阅读数",
      dataIndex: "read_count",
    },
    {
      title: "评论数",
      dataIndex: "comment_count",
    },
    {
      title: "点赞数",
      dataIndex: "like_count",
    },
    {
      title: "操作",
      render: (data) => {
        return (
          <Space size="middle">
            <Button
              type="primary"
              shape="circle"
              icon={<EditOutlined onClick={() => goSend(data)} />}
            />

            <Popconfirm
              title="确认删除?"
              okText="是"
              cancelText="否"
              onConfirm={() => confirm(data)}
            >
              <Button
                type="primary"
                danger
                shape="circle"
                icon={<DeleteOutlined />}
              />
            </Popconfirm>
          </Space>
        );
      },
    },
  ];
  return (
    <Card
      title={
        <Breadcrumb separator=">">
          <Breadcrumb.Item>
            <Link to="/home">首页</Link>
          </Breadcrumb.Item>
          <Breadcrumb.Item>内容管理</Breadcrumb.Item>
        </Breadcrumb>
      }
    >
      {/* 筛选区域 */}
      <Form onFinish={onFinish}>
        <Form.Item label="状态" name="state">
          <Radio.Group onChange={onChange} value={value} defaultValue={-1}>
            <Radio value={-1}>全部</Radio>
            <Radio value={0}>草稿</Radio>
            <Radio value={1}>待审核</Radio>
            <Radio value={2}>审核通过</Radio>
            <Radio value={3}>审核失败</Radio>
          </Radio.Group>
        </Form.Item>
        <Form.Item label="频道" name="chnel_id">
          <Select
            // mode="tags"
            style={{
              width: "20%",
            }}
            placeholder="请选择文章频道"
          >
            {ChannelStore.channelList.map((item) => (
              <Option key={item.id} value={item.id}>
                {item.name}
              </Option>
            ))}
          </Select>
        </Form.Item>
        <Form.Item label="时间" name="time">
          <RangePicker locale={locale} />
        </Form.Item>
        <Form.Item>
          <Button htmlType="submit" type="primary">
            筛选
          </Button>
        </Form.Item>
      </Form>
      {/* 文章列表区域 */}
      <Card title={`根据筛选条件共查询到 ${articleData.count} 条结果`}>
        <Table
          rowKey="id"
          columns={columns}
          dataSource={articleData.list}
          pagination={{
            pageSize: params.per_page,
            total: articleData.count,
            onChange: pageChange,
          }}
        />
      </Card>
    </Card>
  );
}

export default observer(Manage);
